<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Products_in_cart extends Model {

	public $id;
	public $customer;
	public $shoppingcartId;
	public $product;
	public $rayon;
	public $qty;
	public $comments;
	public $price;

	public $offset;
	public $limit;
	public $order_by;
	public $record_count;
	public $join;

	function __construct()
	{
		parent::__construct();
		$this->table = 'products_in_cart';
	}

	function get()
	{
		($this->id) ? $this->db->where('id', $this->id) : NULL ;
		($this->customer) ? $this->db->where('customer', $this->customer) : NULL ;
		($this->shoppingcartId) ? $this->db->where('shoppingcartId', $this->shoppingcartId) : NULL ;
		($this->product) ? $this->db->where('product', $this->product) : NULL ;
		($this->rayon) ? $this->db->where('rayon', $this->rayon) : NULL ;
		($this->qty) ? $this->db->where('qty', $this->qty) : NULL ;
		($this->comments) ? $this->db->where('comments', $this->comments) : NULL ;
		($this->price) ? $this->db->where('price', $this->price) : NULL ;

		($this->limit AND !$this->offset) ? $this->db->limit($this->limit) : NULL ;
		($this->limit AND $this->offset) ? $this->db->limit($this->limit, $this->offset) : NULL ;
		($this->order_by) ? $this->db->order_by($this->order_by) : NULL ;

		if(is_array($this->join))
		{
			foreach($this->join as $join_table => $fields)
			{
				$join_type = (array_key_exists(2, $fields)) ? $fields[2] : 'LEFT' ;
				$this->db->join($join_table, $this->table.'.'.$fields[0] .'='.$join_table.'.'.$fields[1], $join_type);
			}
		}

		$query = $this->db->get($this->table);

		$this->record_count = $query->num_rows();

		if($query->num_rows() > 0)
		{
			if($this->id)
			{
				return $query->row();
			}
			else
			{
				return $query->result();
			}
		}
		else
		{
			return FALSE;
		}
	}

	function save()
	{
		$data = array();
		($this->id) ? $data['id'] = $this->id : NULL ;
		($this->customer) ? $data['customer'] = $this->customer : NULL ;
		($this->shoppingcartId) ? $data['shoppingcartId'] = $this->shoppingcartId : NULL ;
		($this->product) ? $data['product'] = $this->product : NULL ;
		($this->rayon) ? $data['rayon'] = $this->rayon : NULL ;
		($this->qty) ? $data['qty'] = $this->qty : NULL ;
		($this->comments) ? $data['comments'] = $this->comments : NULL ;
		($this->price) ? $data['price'] = $this->price : NULL ;

		($this->id) ? $this->db->where('id', $this->id)->update($this->table, $data) : NULL ;
		(!$this->id) ? $this->db->where('id', $this->id)->insert($this->table, $data) : NULL ;
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE ; 
	}

	function delete()
	{
		($this->id) ? $this->db->where('id', $this->id)->delete($this->table) : NULL ;
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE ; 
	}

	function count_recs()
	{
		return ($this->get()) ? count($this->get()) : 0 ; 
	}
}

/* End of file products_in_cart.php */
/* Location: ./system/application/models/products_in_cart.php */